package com.shujia.day16;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Code02ThreadPool {
    public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(3);


        // submit 将MyThread中的run方法代码块拿到线程池中进行运行  相当于之前Start启动线程
        executorService.submit(new MyThread());
        executorService.submit(new MyThread());
        executorService.submit(new MyThread());
        executorService.submit(new MyThread()); // 当添加第四个任务时，会等前三个任务有一个执行完成，再去执行第四个任务




        // 创建线程的第三种方式
        //  new Callable 并实现其call函数

//        executorService.submit(new Callable<Object>() {
//            @Override
//            public Object call() throws Exception {
//                for (int i = 0; i < 10; i++) {
//                    System.out.println(Thread.currentThread().getName()+":"+i);
//                }
//                return new Object();
//            }
//        });
//
//        executorService.submit(new Callable<Object>() {
//            @Override
//            public Object call() throws Exception {
//                for (int i = 0; i < 10; i++) {
//                    System.out.println(Thread.currentThread().getName()+":"+i);
//                }
//                return new Object();
//            }
//        });

        // 关闭线程池 释放所有的资源
        executorService.shutdown();


    }
}
